package com.jxt.app.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.mina.core.session.IoSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jxt.app.entity.ApsharePostExample;
import com.jxt.app.entity.base.Apdept;
import com.jxt.app.entity.base.Apiphone;
import com.jxt.app.entity.base.ApiphoneExample;
import com.jxt.app.entity.base.Apusers;
import com.jxt.app.entity.base.ApusersExample;
import com.jxt.app.service.ApSharePostService;
import com.jxt.app.service.ApUserService;
import com.jxt.app.service.ApiphoneService;
import com.jxt.app.util.Page;
import com.jxt.mina.session.SessionManager;

@Controller
public class UserController {
	@Resource 
	private ApUserService apusersService;
	@Resource 
	private ApiphoneService apiphoneService;
	/**
	 * 登陆
	 * @param apuserExample
	 * @param requset
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/app/user/login",method=RequestMethod.POST) 
	public String shareList(String username,String password,HttpServletRequest requset, Model model) throws UnsupportedEncodingException{
		

		ApusersExample apuserExample=new ApusersExample();
		List<Apusers> userList=null;
		if(apuserExample!=null){
			apuserExample.setUsername(username);
			apuserExample.setPassword(password);
			apuserExample.setUsername_Indicator(ApusersExample.EXAMPLE_EQUALS);
			apuserExample.setPassword_Indicator(ApusersExample.EXAMPLE_EQUALS);
			userList=apusersService.listModel(apuserExample);
	
		}
		Map<String, Object> resultMap=new HashMap<String, Object>();
			
		if(null!=userList&&userList.size()>0){
			if(userList.get(0).getSysflag()==0){
				resultMap.put("code", "-1001");
				resultMap.put("msg", "您的帐号还没通过审核！");
			}else{
				resultMap.put("code", "1001");
				resultMap.put("user", userList.get(0));
			}
				
		}else{
			resultMap.put("code", "-1001");
		}
		model.addAttribute("result", resultMap);
		return "megList";
	}
	
	
	
	
	@RequestMapping(value="/app/user/getUserByDeptId",method=RequestMethod.POST) 
	public String getUserByDeptId(int deptid,String password,HttpServletRequest requset, Model model) throws UnsupportedEncodingException{
		List<Apusers> userList=apusersService.selecMyVisibleUsers(deptid);
		List<Apdept>  deptList=apusersService.selecMyVisibleApdept(deptid);

		Map<String, Object> resultMap=new HashMap<String, Object>();
		resultMap.put("code", "1001");
		resultMap.put("userList", userList);
		resultMap.put("deptList", deptList);

		model.addAttribute("result", resultMap);
		return "megList";
	}
	
	/**
	 * 注册
	 * @param record
	 * @param requset
	 * @param model
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/app/user/resgister",method=RequestMethod.POST) 
	public void shareList(Apusers record,HttpServletRequest requset,HttpServletResponse response, Model model) throws IOException{
		Map resultMap=new HashMap();
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		int ret=0;
		try{
			if(record!=null){
				
				ApusersExample apuserExample=new ApusersExample();
				apuserExample.setUsername(record.getUsername());
				apuserExample.setUsername_Indicator(ApusersExample.EXAMPLE_EQUALS);
				List<Apusers> userList=apusersService.listModel(apuserExample);
				if(null!=userList&&userList.size()>0){
					resultMap.put("code", "-1001");
					resultMap.put("message", "该用户名已经被注册！");
					
				}else{
					ret=apusersService.insertModel(record);
					resultMap.put("code", "1001");
				}
			}
		}catch(Exception e){
				e.printStackTrace();
				resultMap.put("code", "-1001");
		}

		JSONObject obj = new JSONObject();
 		obj.put("result", resultMap);
 		out.println(obj.toString());

	}
	/**
	 * 获取我可见的用户列表
	 * @param userId
	 * @param requset
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/app/user/getMyUserList",method=RequestMethod.GET) 
	public String getMyseeUserList(String userId,String schoolid,String roleid,String deptid,HttpServletRequest requset, Model model) throws UnsupportedEncodingException{
		Map resultMap=new HashMap();
		int ret=0;
		try{
			
			
			
			
		}catch(Exception e){
				resultMap.put("code", "-1001");
		}



		model.addAttribute("result", resultMap);
		return "megList";
	}
	
	
	
	/**
	 * 用户退出
	 * @param userid
	 * @param requset
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/app/user/logout",method=RequestMethod.POST) 
	public String logout(String userid,HttpServletRequest requset, Model model) throws UnsupportedEncodingException{
		Map<String, Object> resultMap=new HashMap<String, Object>();
		if(userid!=null){
			ApiphoneExample example=new ApiphoneExample();
			example.setUserid(userid);
			example.setUserid_Indicator(ApiphoneExample.EXAMPLE_EQUALS);
			List<Apiphone> iphoneList=apiphoneService.listModel(example);
			
			for(int i=0;i<iphoneList.size();i++){
				Apiphone apiphone=iphoneList.get(i);
				example.setDtoken(apiphone.getDtoken());
				example.setUserid(apiphone.getUserid());
				example.setLogintime(new Date());
				apiphoneService.updateModel(example);
			}
			IoSession session= SessionManager.getInstance().getClientIoSessions().get(userid);
			if(session!=null){	
				session.close(true);
			}
			resultMap.put("code", "1001");
		}else{
			resultMap.put("code", "-1001");
		}
		model.addAttribute("result", resultMap);
		
		return "megList";	
	}
		
	
	
}
